package no.adhocit.timex.controller;

import java.util.List;

import javax.annotation.Resource;

import no.adhocit.timex.domain.Person;
import no.adhocit.timex.domain.Prosjekt;
import no.adhocit.timex.service.MainService;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * Handles and retrieves prosjekt requests
 */
@Controller
@RequestMapping("/prosjekt")
public class ProsjektController {

	protected static Logger logger = Logger.getLogger("controller");
	
	@Resource(name="mainService")
	private MainService mainService;
	
	/**
	 * Handles and retrieves all persons and show it in a JSP page
	 * 
	 * @return the name of the JSP page
	 */
    @RequestMapping(value = "/prosjekter", method = RequestMethod.GET)
    public String getProjects(Model model) {
    	
    	logger.debug("Received request to show all projects");
    	
    	// Retrieve all prosjekter by delegating the call to prosjektService
    	List<Prosjekt> prosjekter = mainService.getAllProsjekt();
    	
    	// Attach prosjekter to the Model
    	model.addAttribute("prosjekt", prosjekter);
    	
    	// This will resolve to /WEB-INF/jsp/prosjektspage.jsp
    	return "prosjektspage";
	}
    
    /**
     * Retrieves the add page
     * 
     * @return the name of the JSP page
     */
    @RequestMapping(value = "/prosjekter/add", method = RequestMethod.GET)
    public String getAdd(Model model) {
    	logger.debug("Received request to show add page");
    
    	// Create new Prosjekt and add to model
    	// This is the formBackingOBject
    	model.addAttribute("prosjektAttribute", new Prosjekt());

    	// This will resolve to /WEB-INF/jsp/prosjektaddpage.jsp
    	return "prosjektaddpage";
	}
 
    /**
     * Adds a new prosjekt by delegating the processing to ProsjektService.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
    @RequestMapping(value = "/prosjekter/add", method = RequestMethod.POST)
    public String add(@ModelAttribute("prosjektAttribute") Prosjekt prosjekt) {
		logger.debug("Received request to add new prosjekt");
		
    	// The "prosjektAttribute" model has been passed to the controller from the JSP
    	// We use the name "prosjektAttribute" because the JSP uses that name
		
		// Call ProsjektService to do the actual adding
		mainService.addProsjekt(prosjekt);

    	// This will resolve to /WEB-INF/jsp/prosjektaddedpage.jsp
		return "prosjektaddedpage";
	}
    
    /**
     * Deletes an existing prosjekt by delegating the processing to ProsjektService.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
    @RequestMapping(value = "/prosjekter/delete", method = RequestMethod.GET)
    public String delete(@RequestParam(value="id", required=true) Integer id, 
    										Model model) {
   
		logger.debug("Received request to delete existing prosjekt");
		
		// Call ProsjektService to do the actual deleting
		mainService.deleteProsjekt(id);
		
		// Add id reference to Model
		model.addAttribute("id", id);
    	
    	// This will resolve to /WEB-INF/jsp/prosjektdeletedpage.jsp
		return "prosjektdeletedpage";
	}
    
    /**
     * Retrieves the edit page
     * 
     * @return the name of the JSP page
     */
    @RequestMapping(value = "/prosjekter/edit", method = RequestMethod.GET)
    public String getEdit(@RequestParam(value="id", required=true) Integer id,  
    										Model model) {
    	logger.debug("Received request to show edit page");
    
    	// Retrieve existing Prosjekt and add to model
    	// This is the formBackingOBject
    	model.addAttribute("prosjektAttribute", mainService.getProsjekt(id));
    	
    	// This will resolve to /WEB-INF/jsp/prosjekteditpage.jsp
    	return "prosjekteditpage";
	}
    
    /**
     * Edits an existing prosjekt by delegating the processing to ProsjektService.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
    @RequestMapping(value = "/prosjekter/edit", method = RequestMethod.POST)
    public String saveEdit(@ModelAttribute("prosjektAttribute") Prosjekt prosjekt, 
    										   @RequestParam(value="id", required=true) Integer id, 
    												Model model) {
    	logger.debug("Received request to update prosjekt");
    
    	// The "prosjektAttribute" model has been passed to the controller from the JSP
    	// We use the name "prosjektAttribute" because the JSP uses that name
    	
    	// We manually assign the id because we disabled it in the JSP page
    	// When a field is disabled it will not be included in the ModelAttribute
    	prosjekt.setId(id);
    	
    	// Delegate to PersonService for editing
    	mainService.editProsjekt(prosjekt);
    	
    	// Add id reference to Model
		model.addAttribute("id", id);
		
    	// This will resolve to /WEB-INF/jsp/prosjekteditedpage.jsp
		return "prosjekteditedpage";
	}
    
}
